package com.apes.fa.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaEnableSituation.CONST_TABLE_NAME)
public class FaEnableSituation extends Model {

    public static final String CONST_TABLE_NAME = "fa_enable_situation";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产启停情况", prefix = "ZQ%(company)", padding = 5)
    @Comment("记录编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fa_card_id", referencedColumnName = "id")
    @Comment("资产卡片")
    private FaCard faCard;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "enabler_id", referencedColumnName = "id")
    @Comment("启用人")
    private PartyRole enabler;

    @Column(name = "enable_date")
    @Comment("启用日期")
    private Date enableDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deactivate_person_id", referencedColumnName = "id")
    @Comment("停用人")
    private PartyRole deactivatePerson;

    @Column(name = "deactivate_date")
    @Comment("停用日期")
    private Date deactivateDate;

    @Column(name = "enablestate")
    @Comment("资产启停状态")
    private String enableState;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @Column(name = "state")
    @Comment("状态(create:已创建,approve:已审核)")
    private String state;

    @Transient
    private String productId;

    public String getProductId() {
        if (faCard!=null) return faCard.getProduct().getId();
        return null;
    }

    @Transient
    private String productName;

    public String getProductName() {
        if (faCard!=null) return faCard.getProduct().getName();
        return null;
    }

    @Transient
    private String company;

    public String getCompany() {
        if (faCard!=null) return faCard.getCompany().getId();
        return null;
    }

    @Transient
    private String businessName;

    public String getBusinessName() {
        if (faCard!=null) return "(QY)资产启用编号 ";
        return null;
    }

}
